package org.jsrml.sv.mob.controller.supplier;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.supplier.command.RemoveContactCo;
import org.jsrml.core.supplier.command.SaveContactCo;
import org.jsrml.core.supplier.entity.Contact;
import org.jsrml.core.supplier.exception.SupplierException;
import org.jsrml.core.supplier.qo.ContactQO;
import org.jsrml.core.supplier.service.ContactService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/contact")
public class ContactController extends BaseController {
	
	@Autowired
	private ContactService contactService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO create(@RequestBody SaveContactCo command) {
		Contact contact = null;
		try {
			contact = contactService.create(command);
		}catch(SupplierException e){
			return createFailResult("-1", e.getMessage(), null);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(contact);
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO modify(@RequestBody SaveContactCo command) {
		Contact contact = null;
		try {
			contact = contactService.modify(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(contact);
	}

	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemoveContactCo command) {
		try {
			contactService.plRemove(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemoveContactCo command) {
		try {
			contactService.remove(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody ContactQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = contactService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Contact contact = contactService.queryUnique(qo);
				return createSuccessResult(contact);
			case 2:
				List<Contact> list = contactService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = contactService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	/**
	 * 关闭供应商联系人
	 * @param contactId
	 * @return
	 */
	@RequestMapping("/close/{contactId}")
	@ResponseBody
	public JSONResultDTO close(@PathVariable String contactId) {
		try {
			contactService.close(contactId);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

}
